/**
 *  Updated 19.09.2011
 *  - escCancel;
 *  - zIndex functions;
 *  - fix movable functions;
 *  - compatiblity with IE;
 */
function popUp(options){
    this.checkIE = checkIE();
    this.minZ = this.calculateMinZ();
    this.active = false;
    this.cururl = null;
    this.bgDiv = null;
    this.popContainer = null;
    this.popDiv = null;
    this.titleDiv = null;
    this.waitingDiv = null;
    this.top = null;
    this.left = null;
    this.movable = false;
    this.preLoadAnimation = true;
    this.move = false;
    this.savePosition = false;
    this.cachedData = null;
    this.locked = 0;
    this.options = {
	title: '',
	data: 'new popup',
	params: {},
	bgDisplay: true,
	type: 'GET',
	callback: function(){},
        onclose: function(){},
	buttons: {},
	bgOpacity: '0.0',
	movable: false,
	preLoadAnimation: true,
	cache: false,
	savePosition: false,
	minimizeButton: false,
        inBackground: false,
        dataType: 'html',
        zPosition: 'auto',
        escCancel: true
    };
    this.postClear = false;
    this.userOptions = {};
    this.cache = false;
    this.minimizeButton = false;
    this.callback = function (){};
    this.maximizeImg = '';
    this.minimizeImg = '';
    this.waitingImg = '';
    this.waitingImgIE = '';
    this.closeImg = '';
    this.btnBackgroundImg = '';
    this.title = 'popUp';
    this.data = '<div style="">\n\
		<h2>Wellcome to "popUp" object!</h2>\n\
		<p>You can configure this popup with folowing options: </p>\n\
		<p>([<b>string or url</b>], [{<b>object</b>} of options])</p>\n\
		<p>OPTIONS:</p>\n\
		<ul>\n\
		    <li>[<b>showTitle</b>] = "true": true or false, show/hide title bar.</li>\n\
		    <li>[<b>title</b>] = "popUp": string of popup title.</li>\n\
		    <li>[<b>left</b>] = automatic: int of left</li>\n\
		    <li>[<b>top</b>] = automatic: int of top</li>\n\
		    <li>[<b>preLoadAnimation</b>] = "true": show or not animation before request complete.</li>\n\
		    <li>[<b>movable</b>] = "false": allow user to move popup.</li>\n\
		    <li>[<b>cache</b>] = "false": try to find loading data in a cache.</li>\n\
		    <li>[<b>bgColor</b>] = "#000": color of backgound.</li>\n\
		    <li>[<b>bgOpacity</b>] = "0.5": opacity of backgound.</li>\n\
		    <li>[<b>hideButton</b>] = "true": show or not close popup button.</li>\n\
		    <li>[<b>minBtn</b>] = "false": show or not minimize popup button.</li>\n\
		    <li>[<b>onclose</b>] = function(){}: function, that running after popup close.</li>\n\
		    <li>[<b>type</b>] = "GET": type of method to load url("POST" or "GET").</li>\n\
		    <li>[<b>params</b>] = {}: object of params to request (if popup get the url in first param)</li>\n\
		    <li>[<b>buttons</b>]: array of buttons object.\n\
			<ul>\n\
			    <li>[<b>value</b>]: string.</li>\n\
			    <li>[<b>clickFunction</b>]: function onclick this button</li>\n\
			    <li>[<b>id</b>]: if you want apply specific id</li>\n\
			</ul>\n\
		    </li>\n\
		    <li>[<b>callback</b>] = function(){}: function, that running after cnontent are loaded.</li>\n\
		</ul>\n\
		</div>';
    this.type = 'GET',
    this.params = {},
    this.bgDisplay = true;
    this.waitingStyle = {
        display: 'none',
        zIndex: this.minZ + 1,
        position:'fixed',
        width:'160px',
        height:'40px',
        backgroundColor:'#000',
        opacity:'0.5',
	textAlign: 'center',
	paddingTop: '20px',
	borderRadius: '5px',
	boxShadow:'0 0 10px #fff'
    };
    this.buttonsDiv = null;
    this.bgStyle = {
        display: 'none',
        zIndex: this.minZ,
        top:'0px',
        left:'0px',
        position:'fixed',
        width:'100%',
        height:'100%',
        backgroundColor:'#000',
        opacity:'0.0'
    };
    this.containerStyle = {
        display: 'none',
        zIndex: this.minZ + 1,
        top:'0px',
        left: '0px',
        position:'fixed',
        margin: '0px',
        boxShadow: '0 4px 6px #CCCCCC',
        borderRadius:'8px',
        backgroundColor:'#fff',
	overflow: 'hidden',
	border: '1px solid rgba(82, 82, 82, 0.7)'
    };
    this.popStyle = {
        display: 'block',
        zIndex: this.minZ + 1,
        position:'relative',
        margin: '0px',
        padding: '10px',
	overflowY:'auto',
        overflowX:'hidden'
    };
    this.titleStyle={
        display:'block',
        zIndex: this.minZ + 1,
        position: 'relative',
        top: '0px',
        left: '0px',
        height: '10px',
	fontFamily: '"lucida grande", "tahoma", "verdana", "arial", sans-serif',
        backgroundColor: '#6d84b4',
        boxShadow: '0 0 0 #3b5998',
        fontWeight: '700',
        color: '#fff',
        padding: '5px',
	paddingBottom: '10px',
        paddingLeft: '10px',
	paddingRight: '30px',
        fontSize: '14px',
	border: '1px solid #3b5998'
    };
    this.btnStyle={
        display:'block',
        zIndex: this.minZ + 1,
        position: 'absolute',
        top: '3px',
        right: '5px',
        width: '12px',
        height: '12px',
	overflow: 'hidden',
        borderTopRightRadius: '10px',
        fontWeight: '600',
        color: '#fff',
        padding: '2px',
        fontSize: '10px',
	cursor: 'pointer'
    };
    this.mnmzStyle={
        display:'none',
        zIndex: this.minZ + 1,
        position: 'absolute',
        top: '3px',
        right: '22px',
        height: '12px',
	overflow: 'hidden',
        borderTopRightRadius: '10px',
        fontWeight: '600',
        color: '#fff',
        padding: '2px',
        fontSize: '10px',
	cursor: 'pointer'
    };
    this.maximizeStyle={
        display:'none',
        zIndex: this.minZ + 1,
        position: 'absolute',
        top: '3px',
        right: '17px',
        width: '12px',
        height: '12px',
	overflow: 'hidden',
        borderTopRightRadius: '10px',
        fontWeight: '600',
        color: '#fff',
        padding: '3px',
        fontSize: '10px',
	cursor: 'pointer'
    };
    
    this.buttonsDivStyle = {
	display: 'block',
	position: 'relative',
	padding: '5px',
	textAlign: 'right',
	background: 'none repeat scroll 0 0 #F2F2F2',
	borderTop: '1px solid #CCCCCC'
    };
    this.miniDivStyle = {
	display: 'none',
	position: 'fixed',
	fontFamily: '"lucida grande", "tahoma", "verdana", "arial", sans-serif',
        backgroundColor: '#6d84b4',
	right: '2px',
	bottom: '0px',
	color: '#fff',
	fontWeight: '700',
	padding: '4px',
	border: '2px solid #868686',
	fontSize: '11px'
    };
    this.btnElementStyle = {
	background: 'none repeat scroll 0 0 #4F6AA3',
	borderBottomColor: '#29447E',
	color: '#FFFFFF',
	fontSize: '13px',
	fontFamily: "'Lucida Grande',Tahoma,Verdana,Arial,sans-serif",
	fontWeight: 'bold',
	marginLeft: '2px',
	marginRight: '2px',
	outline: 'medium none',
	whiteSpace: 'nowrap',
	borderStyle: 'solid',
	lineHeight: 'normal',
	textAlign: 'center',
	textDecoration: 'none',
	height: 'auto',
	borderColor: '#29447E #29447E #1A356E',
	borderWidth: '1px',
	padding: '2px 6px',
	backgroundImage: 'url('+this.btnBackgroundImg+')',
	backgroundRepeat: 'repeat-x',
	cursor: 'pointer'
    };
    this.lockDivStyle = {
	backgroundColor: '#000',
	opacity: '0.1',
	position: 'absolute',
	width: '100%',
	height: '100%',
	display: 'none',
	top: '0px',
	left: '0px',
	zIndex: this.minZ + 2
    };
    this.inBackground = false;
    this.lockDiv = null;
    this.init();
    this.setOptions(options);
    this.history = new Array;
    this.buttons = null;
    this.onclose = function(){};
}
popUp.prototype.calculateMinZ = function(){
    var brosers = getObjectsByClass(popUp);
    var minZ = 1;
    for (var i in brosers){
        if (brosers[i].minZ >= minZ && brosers[i].active == true){
            minZ = brosers[i].minZ + 1;
        }
    }
    return minZ+1;
}
popUp.prototype.setZPosition = function(minZ){
    if (minZ == 'auto')
        minZ = this.minZ;
    this.bgDiv.style.zIndex = minZ;
    this.waitingDiv.style.zIndex = minZ*1 + 1;
    this.popContainer.style.zIndex = minZ*1 + 1;
    this.popDiv.style.zIndex = minZ*1 + 1;
    this.titleDiv.style.zIndex = minZ*1 + 1;
    this.closeBtn.style.zIndex = minZ*1 + 1;
    this.minimizeBtn.style.zIndex = minZ*1 + 1;
    this.minMinimizeBtn.zIndex = minZ*1 + 1;
    this.lockDiv.style.zIndex = minZ*1 + 2;
    this.minZ = minZ;
}
popUp.prototype.preLoad = function(){
    if (this.bgDisplay == true) $(this.bgDiv).fadeIn('fast');
    this.showWaiting();
}
popUp.prototype.load = function(string, options){
        if (this.bgDiv==null && this.popDiv==null)this.init();
	this.data = string || this.data;
	if (typeof(options) == 'object' && options.cache == true){
	    var data = this.getCache(this.data, options);
	    if (data != false){
		this.data = data;
	    }
	}
	if (typeof(options) == 'object' && options.savePosition == true && this.left && this.top){
	    options.left = parseInt(this.popContainer.style.left);
	    options.top = parseInt(this.popContainer.style.top);
	}
	
	this.setOptions(options);
        this.setActions();
	this.createButtons();
	if (this.preLoadAnimation == true) 
	    this.preLoad();
	var that = this;
	if (this.movable == true)
	    this.activateMove();
	else
	    this.deactivateMove();
	if (this.locked == 1) this.unlock();
        if (this.data.match(/^[a-z0-9\/\?\.=&:\-_]{1,500}$/)){
	    this.url = this.data;
            $.ajax({
                type: that.type,
                url: that.data,
		data: that.params,
                dataType: that.dataType,
                success: function(responseText, xmlHttpObj){
                    try{
                        that.popDiv.innerHTML = '';
                        if (that.dataType != 'json'){
                            $(that.popDiv).html(responseText);
                        }else{
                            $(that.popDiv).html(that.data);
                        }  
                        that.history.push({data:that.data, content:responseText,params: that.params});
                        if (that.preLoadAnimation == true) 
                            that.hideWaiting();
                        if (/*that.minimized != true &&*/ that.inBackground != true)
                            that.show();
                        that.callback(responseText);
                    }catch(error){
//                        showObject(error);
                        that.setContent(error.message); 
                        that.show();
                    }
                },
                error: function(xmlHttp, textStatus, errorThrown){
		    that.hideWaiting();
                    that.setContent(getErrorText(xmlHttp, textStatus, errorThrown));    
                    that.show();
		}
            });
        }else{
	  this.popDiv.innerHTML = '';
	  $(that.popDiv).html(this.data);
	  this.history.push({data:this.data,content:this.data, params: {}});
	  this.hideWaiting();
          if (/*that.minimized != true && */that.inBackground != true)
                this.show(); 
	  this.callback();
        }
        this.setZPosition(this.calculateMinZ());
    };
popUp.prototype.setContent = function(string){
    if (string){
        $(this.popDiv).html(string);
        return true;
    }else{
        return false;
    }
}
popUp.prototype.getCache = function(url, options){
    for (var object in this.history){
	var currentParams = (this.history[object].params)?(this.history[object].params):({});
	var newParams = (options.params)?(options.params):({});
	var flagParams = compareObjects(currentParams, newParams);
	if (this.history[object].data == url && flagParams){
	    var content = this.history[object].content;
		return content;
	}    
    }
    return false; 
} 
popUp.prototype.setUserOptions = function(options){
    if (typeof (options) == 'object' && typeof(options.from) == 'undefined')
	this.userOptions = options;
}
popUp.prototype.reload = function(){
    if (this.url){
	var options = this.userOptions;
	    options.cache = false;
	    options.from = 'me';
	    options.left = parseInt(this.popContainer.style.left);
	    options.top = parseInt(this.popContainer.style.top);
            options.inBackground = true;
	if (isScript('waiting.js')){
	    var that = this;
	    this.meWaiting = new waiting();
	    this.meWaiting.load(this.popContainer);
	    var thisCallback = function(){};
	    if (typeof(options.callback)=='function')
		thisCallback = options.callback;
	    
	    options.callback = function(){
		thisCallback();
		that.meWaiting.hide();
	    }
	}
	this.load(this.url, options);
    }
}
popUp.prototype.createButtons = function(buttons){
    var buttons = buttons || this.buttons;
    this.buttonsDiv.innerHTML = '';
    if (typeof(buttons) == 'object' && buttons != null){
	
	for (var button in buttons){
	    if (typeof (buttons[button] == 'object')){
		var btnElementId = '';
		if (buttons[button].id){
		    btnElementId = buttons[button].id;
		}else{
		    var uid = Math.floor(Math.random()*0x100000).toString(16);
		    btnElementId = 'button_' + uid;
		}
		var btnElementStyle = this.btnElementStyle;
		if (typeof(buttons[button].style) == 'object')
		    btnElementStyle = buttons[button].style;
		var btnElement = cE('button', {id:btnElementId, className: 'popup_button'}, btnElementStyle);
		if (typeof(buttons[button].clickFunction) == 'function')
		    btnElement.onclick = buttons[button].clickFunction;
		var value = 'button';
		if (buttons[button].value)
		    value = buttons[button].value;
		    btnElement.innerHTML = value;   
		this.buttonsDiv.appendChild(btnElement);
	    }
	}
	this.buttonsDiv.style.display = 'block';
    }else{
	this.buttonsDiv.style.display = 'none';
    }
};
popUp.prototype.hideButtons = function(num, animate){
    var buttons = this.buttonsDiv.getElementsByTagName('button');
    var animate = animate || false;
    if (num){
	if (buttons[num-1])
	    if ($ && animate == true)
		$(buttons[num-1]).slideUp('normal');
	    else
		buttons[num-1].style.display = 'none';
            return true;
    }else{
        if ($ && animate == true)
            $(buttons).slideUp('normal');
        else
            for (var i=0; i<buttons.length; i++){
                buttons[i].style.display = 'none';
            }
        return true;
    }
}
popUp.prototype.showButtons = function(num, animate){
    var buttons = this.buttonsDiv.getElementsByTagName('button');
    var animate = animate || false;
    if (num){
	if (buttons[num-1])
            if ($ && animate == true)
		$(buttons[num-1]).slideDown('normal');
	    else
		buttons[num-1].style.display = 'inline-block';
    }else{
        if ($ && animate == true)
            $(buttons).slideDown('normal');
        else
            for (var i=0; i<buttons.length; i++){
                buttons[i].style.display = 'inline-block';
            }
        return true;
    }
}
popUp.prototype.init = function(){
        var that = this;
        var uid = Math.floor(Math.random()*0x100000).toString(16);
	var waitingId = 'waitingDiv_' + uid;
        var bgId = 'bgDiv_' + uid;
	var contId = 'contDiv_' + uid;
        var popId = 'popDiv_' + uid;
        var btnId = 'btnCls_' + uid;
	var btnMinId = 'btnMnmz_' + uid;
	var minDivId = 'minDiv_' + uid;
        var titleId = 'titleDiv_' + uid;
	var buttonsDivId = 'butonsDiv_' + uid;
	var minimizedBtnId = 'minimizedBtn_' + uid;
	var minimizedMinBtnId = 'minimizedMinBtn_' + uid;
	var minTitleDivId = 'minTitleDiv_' + uid;
	var lockDivId = 'lockDiv_' + uid;
	
	this.waitingDiv = cE('div',{id:waitingId, className: 'popup_waiting_div'},this.waitingStyle);
        this.waitingDiv.innerHTML = '<img src="' + this.waitingImg +'"/>';
        this.bgDiv = cE('div',{id:bgId, className: 'popup_bg_div'},this.bgStyle);
	this.popContainer = cE('div',{id:contId, tabIndex: '0', className: 'popup_pop_container'},this.containerStyle);
        this.popDiv = cE('div',{id:popId, className: 'popup_content_div'},this.popStyle);
	this.buttonsDiv = cE('div', {id: buttonsDivId, className: 'popup_buttons_div'}, this.buttonsDivStyle);
        this.titleDiv = cE('div',{id:titleId, className: 'popup_title_div'},this.titleStyle);
        this.titleDiv.innerHTML = this.title;
        this.closeBtn = cE('div', {id:btnId, className: 'popup_close'},this.btnStyle);
        this.closeBtn.innerHTML = '<img src="' + this.closeImg + '"/>';
	this.minimizeBtn = cE('div', {id:btnMinId, className: 'popup_minimize'},this.mnmzStyle);
        this.minimizeBtn.innerHTML = '<img src="' + this.minimizeImg + '"/>';
	this.lockDiv = cE('div', {id: lockDivId, className: 'popup_lock'}, this.lockDivStyle);
	this.miniDiv = cE('div', {id:minDivId, className:'popup_minimized_div'}, this.miniDivStyle);
	this.minTitleDiv = cE('div', {id:minTitleDivId, className: 'popup_minimized_title'},{display:'inline-block', marginRight: '55px'});
	this.minTitleDiv.innerHTML = this.title;
	this.minCloseBtn = cE('div', {id:minimizedBtnId, className: 'popup_minimized_close'},this.btnStyle);
	this.minCloseBtn.innerHTML = '<img src="' + this.closeImg + '"/>';
	this.minMinimizeBtn = cE('div', {id:minimizedMinBtnId, className: 'popup_minimized_minimize'},this.maximizeStyle);
	this.minMinimizeBtn.innerHTML = '<img src="' + this.maximizeImg + '"/>';
	this.miniDiv.appendChild(this.minTitleDiv);
	this.miniDiv.appendChild(this.minCloseBtn);
	this.miniDiv.appendChild(this.minMinimizeBtn);
        this.popDiv.innerHTML = '';
	this.popContainer.appendChild(this.titleDiv);
	this.popContainer.appendChild(this.closeBtn);
	this.popContainer.appendChild(this.minimizeBtn);
        this.popContainer.appendChild(this.popDiv);
        this.popContainer.appendChild(this.buttonsDiv);
	this.popContainer.appendChild(this.lockDiv);
        this.popDiv.innerHTML = this.data;
	document.body.appendChild(this.miniDiv)
        document.body.appendChild(this.bgDiv);
        document.body.appendChild(this.popContainer);
	document.body.appendChild(this.waitingDiv);
        
    };

popUp.prototype.deactivateMove = function (){
    var that = this;
    that.titleDiv.onmousedown = function(){
    }
    that.titleDiv.onmouseup = function(){
    }
    that.titleDiv.onmousemove = function(){
    }
    that.popContainer.onmouseup = function(){

    }
}
popUp.prototype.activateMove = function(){
    var that = this;
    that.movePop = function(event){
	var event = event || window.event;
	var moveX = that.currentX + (event.clientX - that.cursorPositionX);
	var moveY = that.currentY + (event.clientY - that.cursorPositionY);
	if (that.moveNow == true){
	    that.popContainer.style.left = moveX + 'px';
	    that.popContainer.style.top = moveY + 'px';
	}
    }
    that.upMouse = function (e){
	that.titleDiv.style.cursor = 'default';
	that.moveNow = false;
	that.left = parseInt(that.popContainer.style.left);
	that.top = parseInt(that.popContainer.style.top);
	$(document).unbind('mouseup', that.upMouse);
	$(document).unbind('mousemove', that.movePop);
    }
    that.titleDiv.onmousedown = function(e){
	var e = e || window.event;
	this.style.cursor = 'move';
	that.moveNow = true;
	that.currentX = parseInt(that.popContainer.style.left);
	that.currentY = parseInt(that.popContainer.style.top);
	that.cursorPositionX = e.clientX;
	that.cursorPositionY = e.clientY;
	$(document).bind('mousemove', that.movePop);
	$(document).bind('mouseup', that.upMouse);
    } 
}    
popUp.prototype.show = function(){
	var that = this;
        if (this.minimized == true) this.maximize();
        if (this.bgDiv==null && this.popContainer==null)this.init();
	$(this.waitingDiv).css({display: 'none'});
	$(this.popContainer).fadeIn('fast', function(){that.active = true});
	this.centralize();
        $(this.popContainer).focus();
};
popUp.prototype.minimize = function(){
    this.minimized = true;
    $(this.popContainer).fadeOut('fast');
    var right = 2 + this.getRightPosition();
    $(this.miniDiv).css({right:right+'px'});
    $(this.miniDiv).fadeIn('fast');
}
popUp.prototype.getRightPosition = function(){
    var right = 0;
    var objects = $('*').filter(function() { 
	return $(this).css('position') == 'fixed' && $(this).css('display') == 'block' && parseInt($(this).css('bottom')) == '0' && parseInt($(this).css('right')) < '140';
    }); 
    if (objects.length>0){
	for (var i=0; i<objects.length; i++){
	    right = right + parseInt(objects[i].style.right);
	    right = right + objects[i].clientWidth*1;
	}
	right = right*1+4;
    }
    
    return right;
}
popUp.prototype.moveMyBrotherRight = function(){
    var myWidth = this.miniDiv.clientWidth + parseInt(this.miniDiv.style.right);
    var objects = $('*').filter(function() { 
	return $(this).css('position') == 'fixed' && $(this).css('display') == 'block' && parseInt($(this).css('bottom')) == '0' && parseInt($(this).css('right')) > myWidth;
    });
    if (objects.length>0){
	$(objects).animate({right:-myWidth});
    }
    
}
popUp.prototype.maximize = function(){
    var that = this;
    this.minimized = false;
    $(this.popContainer).fadeIn('fast');
    
    $(this.miniDiv).fadeOut('fast', function(){
	that.moveMyBrotherRight();
    });
}
popUp.prototype.hideMinimized = function(){
    $(this.miniDiv).fadeOut('fast');
}
popUp.prototype.hide = function(){
	var that = this;
	that.hideWaiting();
	that.hideMinimized();
        $(this.bgDiv).fadeOut('fast');
        $(this.popContainer).fadeOut('fast',function(){
	    that.active = false;
            that.minimized = false;
	    that.onclose();
	    if (that.postClear == true){
		that.clearMe();
	    }
	});
    },
popUp.prototype.setOptions = function(options){
	if (typeof(options) == 'object'){
		var that = this;
		that.setUserOptions(options);
                if (options.escCancel == false) this.escCancel = options.escCancel; else this.escCancel = this.options.escCancel;
		if(options.postClear == false){
			this.postClear = false;
		}
		if(options.postClear == true){
			this.postClear = true;
		}
		if(options.bgDisplay == false){
			this.bgDisplay = false;
			this.bgDiv.style.display = 'none';
			}
		if(options.bgDisplay == true){
			this.bgDisplay = true;
			if (this.active == true)this.bgDiv.style.display = 'block';
			}
                if (options.inBackground) this.inBackground = options.inBackground; else this.inBackground = this.options.inBackground;
                if (options.dataType) this.dataType = options.dataType; else this.dataType = this.options.dataType;
                if (options.zPosition) {this.zPosition = options.zPosition;} else {this.zPosition = this.options.zPosition;}
                    this.setZPosition(this.zPosition);
		if (options.top) this.top = options.top;
		if (options.left) this.left = options.left;
		if (options.bgOpacity)this.bgDiv.style.opacity = options.bgOpacity;
		    else
			this.bgDiv.style.opacity = this.options.bgOpacity;
		if(options.bgColor)this.bgDiv.style.backgroundColor = options.bgColor;
		if(options.width){
		    this.popContainer.style.width = options.width;
		    this.width = options.width;
		};
		if(options.height){
		    this.popContainer.style.height = options.height;
		    this.height = options.height;
		};
		if(options.type)this.type = options.type; else this.type = this.options.type;
		if(options.preLoadAnimation == false || options.preLoadAnimation == true)this.preLoadAnimation = options.preLoadAnimation; else this.preLoadAnimation = this.options.preLoadAnimation;
		if(options.params && typeof(options.params)=='object')this.params = options.params; else this.params = this.options.params;
		if(options.movable) this.movable = options.movable; else  this.movable = this.options.movable;
		if(options.cache) {this.cache = options.cache} else  this.cache = this.options.cache;
		if(options.showTitle == true) {
		    this.titleDiv.style.display = 'block';
		}
		if(options.showTitle == false) {
		    this.titleDiv.style.display = 'none';
		}
		
		if(options.savePosition) {this.savePosition = options.savePosition} else  this.savePosition = this.options.savePosition;
                if(typeof(options.title) == 'string'){
                    this.title = options.title;
                    this.titleDiv.innerHTML = options.title;
		    this.minTitleDiv.innerHTML = options.title;
                }
		if(options.title == false){
                    this.titleDiv.style.display = 'none';
                }
		if(options.hideButton == false){
			
			this.closeBtn.style.display = 'none';
		}
		if(typeof(options.callback) == 'function'){
			this.callback = options.callback;
		}else{
                    this.callback = this.options.callback;
                }
		if(typeof(options.onclose) == 'function'){
			this.onclose = options.onclose;
		}else{
                    this.onclose = this.options.onclose;
                }
		if(options.minBtn == false){
			this.minimizeBtn.style.display = 'none';
			this.minMinimizeBtn.style.display = 'none';
		}
		if(options.minBtn == true){
			this.minimizeBtn.style.display = 'block';
			this.minMinimizeBtn.style.display = 'block';
		}
		if(options.hideButton == true){
			this.closeBtn.style.display = 'block';
		}
		if (typeof(options.buttons) == 'object')
		    this.buttons = options.buttons;
		else
		    this.buttons = null;
	}
    };
popUp.prototype.checkFB = function(){
    if (typeof(FB) != 'undefined' && typeof(FB.Canvas) != 'undefined'){
	var pageInfo = FB.Canvas.getPageInfo();
	if (pageInfo.clientHeight != 0 || pageInfo.clientWidth != 0){
	    return pageInfo;
	}else{
	    return false;
	}
    }else{
	return false;
    }
}
popUp.prototype.centralize = function(el){
	var el = el || this.popContainer;
	var maxHeight = (window.innerHeight)?(window.innerHeight):(document.documentElement.clientHeight)-50;//this.bgDiv.clientHeight-50-this.titleDiv.clientHeight;
	var maxWidth  = (window.innerWidth)?(window.innerWidth):(document.documentElement.clientWidth)-50;
	var pageInfo = this.checkFB();
	if (pageInfo){
	    var maxFBHeight = pageInfo.clientHeight;
	    var currentScroll = pageInfo.scrollTop;
	   
	}
	var buttonsHeight = 0;
	    if (this.buttonsDiv.style.display != 'none')
		buttonsHeight = (this.buttonsDiv.innerHeight)?(this.buttonsDiv.innerHeight):(this.buttonsDiv.clientHeight);
        el.style.maxHeight = (maxHeight-25)+'px';
	el.style.maxWidth = (maxWidth-25)+'px';
	if (this.left && this.top && el == this.popContainer)
	    {
		var left = this.left;
		var top = this.top;
	    }else{
		
		var left = ((maxWidth/2)-(el.clientWidth/2)).toFixed(0);
		var top = 20;
		if (maxFBHeight){
		    top = (maxFBHeight/2*1 - el.clientHeight/2*1).toFixed(0);
		    if (currentScroll)
			top = top*1 + currentScroll*1;
		    if (top<100)
			top = 100;
		}else{
		    top = ((maxHeight/2)-(el.clientHeight/2)).toFixed(0);
		}
	    }
	el.style.left = left+'px';
	el.style.top = top+'px';
	if (el == this.popContainer)
	    {
		if (!this.width){
		    this.popContainer.style.width = (this.popContainer.clientWidth)?(this.popContainer.clientWidth):(this.popContainer.offsetWidth*1-20) + 'px';
		}
		if (!this.height){
		    this.popDiv.style.maxHeight = (maxHeight-top-100-buttonsHeight)+'px';
		}
	    }
    }
popUp.prototype.setActions = function(){
    var that = this;
    var btnId = this.closeBtn.id;
    var minBtnId = this.minimizeBtn.id;
    var minCloseBtnId = this.minCloseBtn.id;
    var minMiniId = this.minMinimizeBtn.id;
    var miniDivId = this.miniDiv.id;
    
    document.getElementById(btnId).onclick = function(){that.hide()};
    document.getElementById(btnId).getElementsByTagName('img')[0].onclick = function(){that.hide()}; 
    document.getElementById(minBtnId).onclick = function(){that.minimize()};
    document.getElementById(minCloseBtnId).onclick = function(){that.hide()};
    document.getElementById(minMiniId).onclick = function(){that.maximize()};
    if (this.escCancel == true){
//        this.popContainer.onmouseover = function (){
//            this.focus();
//        }
//        this.popContainer.onmouseout = function (){
//            this.blur();
//        }
        this.popContainer.onkeypress = function (event){
            var event = event || window.event;
            if (event.keyCode == 27){
                that.hide();
            }
        }
    }
}
popUp.prototype.back = function(reload){
    var reload = reload || true;
    if (this.history[this.history.length-2]){
        if (reload==true){
            var data = this.history[this.history.length-2].data;
            this.history.splice(this.history.length-2,2);
            this.load(data);
        }else{
            var data = this.history[this.history.length-2].content;
            this.history.splice(this.history.length-2,2);
            this.load(data);
        }
    }
}    
popUp.prototype.showWaiting = function(){
    $(this.waitingDiv).fadeIn('fast');
    this.centralize(this.waitingDiv);
} 
popUp.prototype.hideWaiting = function(){
    $(this.waitingDiv).fadeOut('fast');
} 
popUp.prototype.lock = function(){
    if (this.active == true){
        $(this.lockDiv).fadeIn('normal');
    }else{
        this.lockDiv.style.display = 'block';
    }
    this.locked = 1;
}
popUp.prototype.unlock = function (){
    if (this.active == true){
        $(this.lockDiv).fadeOut('normal');
    }else{
        this.lockDiv.style.display = 'none';
    }
    this.locked = 0;
}
popUp.prototype.toggleLock = function (){
    if (this.locked == 1){
	this.unlock();
    }else{
	this.lock();
    }
}
popUp.prototype.clearMe = function(){
    this.popDiv.innerHTML = '';
    return true;
}